<?php
namespace App\Http\Controllers;
/**
 * 
 * 团控制器
 * 
 * @author maqu
 *
 */
use App\Models\Article;
use App\Models\Topic;
use App\Services\GoodsService;
use App\Services\PintuanService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class TuanController extends BaseController {

    /**
     *
     * 团详情
     *
     * @param $tuan_id 团ID
     * @return mixed
     */
    public function getDetail($tuan_id){

        //后台借用下接口

        //参数编辑
        $tuan_id = intval($tuan_id);
        $user_id = intval(Input::get('user_id',0));
        $pintuan_type = intval(Input::get('pintuan_type',0)) ;
        if (!$tuan_id || !$pintuan_type) {
            return $this->args_invalid();
        }

        //数据
        //1 拼团详情
        //2 我是否已参加
        //3 分享地址
        //4 团数据

        $data = [
            'share_url'=> config('maqu.WAP_URL').'/html/tuan/tuan.html?pintuan_type='.$pintuan_type.'&tuan_id='.$tuan_id,
            'pintuan_info' => [],
            'tuan_info' =>[],
            'tuan_data'=>[],
            'status'=>[
                'lefttime'=>0,
                'joined'=>0
            ]
        ];

        //获取拼团数据
        $pintuanService= new PintuanService();
        $result =$pintuanService->getTuanDetail($tuan_id,$user_id,$pintuan_type);

        if(!$result['result']){
            return $this->failure($result['msg']);
        } else {
            $data['pintuan_info']=$result['data']['pintuan_info'];
            $data['tuan_info']=$result['data']['tuan_info'];
            $data['tuan_data']=$result['data']['tuan_data'];
            $data['hongbao_info']=$result['data']['hongbao_info'];
            $data['status']=$result['data']['status'];

        }

        //编辑结果
        return $this->success($data);
    }

    /**
     *
     * @param firstrow
     * @param pagesize
     *
     * @return json
     */
    public function getList($user_id){

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        $nav = Input::get('nav','all');

        //获取拼团数据
        $pintuanService= new PintuanService();
        $result =$pintuanService->getTuanList2($user_id,$nav,$firstrow,$pagesize);
        if(!$result['result']){
            return $this->failure($result['msg']);
        } else {
            $data=$result['data'];
        }

        //编辑结果
        return $this->success($data);

    }

}
